//
//  VevifyViewController.m
//  DailyRecord
//
//  Created by Gin on 2022/4/16.
//

#import "VevifyViewController.h"
#import "UIView+Style.h"
#import "UITextField+PlaceHolder.h"
#import "UIImage+Resize.h"
#import "NetWorkService.h"
#import "BusinessPublicParamsManager.h"

@interface VevifyViewController ()

@property (nonatomic, strong) UITextField *verifyTf;

@property (nonatomic, strong) UIButton *submitBtn;

@end

@implementation VevifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setCustomNavigationTitle: @"验证"];
    [self setDefaultVerifyCode];
}

- (void)initUI{
    [self.view addSubview:self.verifyTf];
    [self.view addSubview:self.submitBtn];
}

- (void)setUpContraints{
    [self.verifyTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kLeftSpace);
        make.right.equalTo(self.view.mas_right).offset(-kRightSpace);
        make.top.mas_equalTo(kNavigationBarHeight + kTopSpace);
        make.height.mas_equalTo(kSmallButtonHeight);
    }];
    
    [self.submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-kRightSpace);
        make.left.mas_equalTo(kLeftSpace);
        make.height.mas_equalTo(kLargeButtonHeight);
        make.top.equalTo(self.verifyTf.mas_bottom).offset(kLargeButtonHeight);
    }];
}

- (void)submitVerify{
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:[BusinessPublicParamsManager queryPublicParams]];
    [params removeObjectForKey:@"uid"];
    [MBProgressHUD showMessage:kInRequestText toView:self.view];
    [NetWorkService serviceGetWithPath:[NSString stringWithFormat:@"%@/%@", kQueryCondition, kVerifyTable] parameters:params success:^(id  _Nullable responseObj) {
        [MBProgressHUD hideHUDForView:self.view];
        [self verifyWithCode:[responseObj[kRequestResponseDataKey] firstObject][kVerifyCodeKey]];
    } failure:^(NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:self.view];
        NSString *verifyCode = [[NSUserDefaults standardUserDefaults] stringForKey:kVerifyCodeKey];
        [self verifyWithCode:verifyCode];
    }];
}

- (void)setDefaultVerifyCode{
    NSUserDefaults *uf = [NSUserDefaults standardUserDefaults];
    [uf setObject:kDefaultVerifyCode forKey:kVerifyCodeKey];
    [uf synchronize];
}

- (void)verifyWithCode:(NSString *)code{
    if ([self.verifyTf.text isEqualToString:code]) {
        NSUserDefaults *uf = [NSUserDefaults standardUserDefaults];
        [uf setObject:@(YES) forKey:kFinishCurrentModule];
        [uf synchronize];
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationToMainService object:nil userInfo:nil];
    }
}

#pragma mark -- setter&&getter

- (UITextField *)verifyTf{
    if (!_verifyTf) {
        _verifyTf = [UITextField new];
        _verifyTf.font = kInfoFont;
        _verifyTf.placeholder = @"请输验证码";
        [_verifyTf setPlaceholderFont:_verifyTf.font];
        [_verifyTf styleBorderWidthAndColor];
    }
    return _verifyTf;
}

- (UIButton *)submitBtn{
    if (!_submitBtn) {
        _submitBtn = [UIButton new];
        [_submitBtn setTitle:@"提交" forState:UIControlStateNormal];
        [_submitBtn setBackgroundImage:[[UIImage imageNamed:@"btn_bg"] resizeWithCenter] forState:UIControlStateNormal];
        @weakify(self);
        [[_submitBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            [self submitVerify];
        }];
    }
    return _submitBtn;
}

@end
